/**
 * 
 */
package org.ejc.api.statistics;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;

import org.apache.log4j.Category;
import org.ejc.api.registration.RegistrationAPIBean;
import org.ejc.exception.EJCPersistenceException;
import org.ejc.persist.model.CountryCode;
import org.ejc.persist.model.Registrant;
import org.ejc.persist.model.RegistrantStatusType;
import org.ejc.util.EJCUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
/**
 * @author Brian Boyle
 * 
 * @ejb.bean type="Stateless" name="StatisticsAPI"
 *           description="StatisticsAPI session bean"
 *           local-jndi-name="org.ejc/StatisticsAPILocalHome"
 *           jndi-name="org.ejc/StatisticsAPIHome" view-type="both"
 * 
 * @ejb.ejb-ref ejb-name="StatisticsAPI" ref-name="ejb/StatisticsAPI"
 *              view-type="both"
 * 
 * @ejb.transaction type="RequiresNew"
 * @ejb.util generate = "physical"
 */
public class StatisticsAPIBean implements SessionBean {

	Category log = Category.getInstance(StatisticsAPIBean.class);
	
	/**
	 * @return
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Map getCountryStatistics()
	{
		Session hsession = null;
		HashMap countryStats = new HashMap();
		try {
			hsession = EJCUtils.getHibernateSession();
			Criteria crit = hsession.createCriteria(CountryCode.class);
			ArrayList countries = (ArrayList) crit.list();
			if(countries != null){
				Iterator iter = countries.iterator();
				while(iter.hasNext()){
					CountryCode country = (CountryCode)iter.next();

					Query q = hsession.createQuery("select count(*) from Registrant reg where reg.address.countryCode.countryId=?");
					Integer count = (Integer) q.setParameter(0,country.getCountryId()).uniqueResult();
					if(count > 0){
						countryStats.put(country.getCountryIsoCode(), count);
					}
				}
			}
			
			
		} catch (EJCPersistenceException epe) {
			epe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return countryStats;
	}
	
	/**
	 * @return
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Map getGuestStatus()
	{
		Session hsession = null;
		HashMap guestStatusStats = new HashMap<String, Integer>();
		try {
			hsession = EJCUtils.getHibernateSession();
			Criteria crit = hsession.createCriteria(RegistrantStatusType.class);
			ArrayList statusTypes = (ArrayList) crit.list();
			Iterator iter = statusTypes.iterator();
			while(iter.hasNext()){
				RegistrantStatusType statusType = (RegistrantStatusType) iter.next();
				Query q = hsession.createQuery("select count(*) from ConventionRegistrant convReg where convReg.registrantStatusType.id=?");
				Integer count = (Integer) q.setParameter(0,statusType.getId()).uniqueResult();
				log.debug("BROUGHT BACK ["+count+"] results for ["+statusType.getStatusDesc()+"]");
				guestStatusStats.put(statusType.getStatusDesc(), count);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return guestStatusStats;
	}
	
	/**
	 * @return
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Map getGuestLengthOfStayStats()
	{
		Session hsession = null;
		HashMap lengthOfStayStats = new HashMap<String, Integer>();
		try {
			hsession = EJCUtils.getHibernateSession();
			for(int i = 1;i <= 7;i++){
				
				Query q = hsession.createQuery("select count(*) from ConventionRegistrant convReg where convReg.lengthOfStay = ?");
				Integer count = (Integer) q.setParameter(0,i).uniqueResult();
				log.debug("BROUGHT BACK ["+count+"] results for ["+i+"] days");
				lengthOfStayStats.put(Integer.toString(i), count);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return lengthOfStayStats;
	}
	
	/**
	 * @return
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Map getGuestAgeStats()
	{
		Session hsession = null;
		HashMap ageStats = new HashMap<String, Integer>();
		try {
			hsession = EJCUtils.getHibernateSession();
			String[] ages = {"N/A", "1-12", "13-18", "18-25", "25+"};
			for(int i = 0;i <= ages.length;i++){
				
				Query q = hsession.createQuery("select count(*) from Registrant reg where reg.ageBracket = ?");
				Integer count = (Integer) q.setParameter(0,ages[i]).uniqueResult();
				log.debug("BROUGHT BACK ["+count+"] results for ["+ages[i]+"] age");
				ageStats.put(ages[i], count);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return ageStats;
	}
	
	/**
	 * @return
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Map getMoneyAmountPerCountryStats()
	{
		Session hsession = null;
		HashMap moneyStats = new HashMap<String, Integer>();
		try {
			hsession = EJCUtils.getHibernateSession();
			Criteria crit = hsession.createCriteria(CountryCode.class);
			ArrayList countries = (ArrayList) crit.list();
			if(countries != null){
				Iterator iter = countries.iterator();
				while(iter.hasNext()){
					CountryCode country = (CountryCode)iter.next();
					
					Query q = hsession.createQuery("select sum(transaction.transactionAmount) from " +
					"Transaction transaction where transaction.registrant.address.countryCode.countryIsoCode = ?");
					Double sum = (Double) q.setParameter(0,country.getCountryIsoCode()).uniqueResult();
					log.debug("BROUGHT BACK ["+sum+"] results for ["+country.getCountryIsoCode()+"] country");
					if(sum > 0.0){
						moneyStats.put(country.getCountryIsoCode(), sum);
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return moneyStats;
	}

	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		
	}

	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		
	}

	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		
	}

	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		
	}


}
